from Devices.Chiot.BsDevs.ComDev.ComDevParser import ComDevParser

class MotionParser(ComDevParser):
    def ParseHeartbeat(msgValue, fullStatus):
        if not "stateValue" in msgValue:
            return True
        stateValue = msgValue["stateValue"]

        ret, value = MotionParser.GetIntProperty(stateValue, "pir", "status")
        if ret:
            fullStatus.sensorStatus = value
            fullStatus.changeTime = fullStatus.changeTime = QDateTime.currentSecsSinceEpoch()

        ret, value = MotionParser.GetIntProperty(stateValue, "electric", "powerWarn")
        if ret:
            fullStatus.powerWarn = value

        ret, value = MotionParser.GetIntProperty(stateValue, "electric", "batteryPercentage")
        if ret:
            fullStatus.batteryPercentage = value

        ret, value = MotionParser.GetIntProperty(stateValue, "checkStatus", "status")
        if ret:
            fullStatus.checkStatus = value

        ret, value = MotionParser.GetIntProperty(stateValue, "checkStatusTime", "timestamp")
        if ret:
            fullStatus.checkStatusTime = value

        if "reportMinInterval" in stateValue:
            fullStatus.reportInterval = int(stateValue['reportMinInterval'])
        return True

    def PackSetConfigReportInterval(interval):
        msgvalue = {}
        msgvalue["reportInterval"] = int(interval)

        obj = {}
        jsonDoc = ComDevParser.PackBase(obj, msgvalue, "setConfig")
        return json.dumps(jsonDoc, separators=(',', ':'))